package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import com.guldan.core.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.search.Pagination;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.dictionary.DictionaryDTO;
import com.guldan.jaina.dto.dictionary.DictionarySearchDTO;
import com.guldan.jaina.dto.projectcategory.ProjectCategoryDTO;
import com.guldan.jaina.dto.projectcategory.ProjectCategorySearchDTO;
import com.guldan.jaina.utils.DictionaryDomainConvertUtils;
import com.guldan.jaina.utils.ProjectCategoryDomainConvertUtils;
import com.guldan.mapper.db.entity.Dictionary;
import com.guldan.mapper.db.entity.DictionaryExample;
import com.guldan.mapper.db.entity.ProjectCategory;
import com.guldan.mapper.db.entity.ProjectCategoryExample;
import com.guldan.mapper.db.mapper.ProjectCategoryMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class ProjectCategoryManager extends BaseManager {

    @Autowired
    private ProjectCategoryMapperExt projectCategoryMapperExt;

    //获取单个项目分类信息
    public ProjectCategoryDTO get(Integer projectCategoryId) {
        ProjectCategorySearchDTO searchDto = new ProjectCategorySearchDTO();
        searchDto.setProjectCategoryId(projectCategoryId);
        List<ProjectCategoryDTO> dtoList = all(searchDto);
        if (dtoList != null && dtoList.size() > 0)
            return dtoList.get(0);
        return null;
    }

    //判断某个区划是否已经存在
    private Boolean checkExist(String projectCategoryCode) {
        ProjectCategoryExample example = new ProjectCategoryExample();
        ProjectCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andProjectCategoryCodeEqualTo(projectCategoryCode);
        criteria.andIsDeleteEqualTo(false);
        int count = projectCategoryMapperExt.countByExample(example);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    //新增/更新单个项目分类，传入：ProjectCategoryDTO
    public ProjectCategoryDTO save(ProjectCategoryDTO dto) throws BusinessException {


        ProjectCategory entity;
        if (dto.getProjectCategoryId() != null && dto.getProjectCategoryId() > 0) {

            entity = projectCategoryMapperExt.selectByPrimaryKey(dto.getProjectCategoryId());
            entity.setProjectCategoryCode(dto.getProjectCategoryCode());
            entity.setProjectCategoryName(dto.getProjectCategoryName());
            entity.setProjectCategoryNameZw(dto.getProjectCategoryNameZw());
            entity.setProjectCategoryParentId(dto.getProjectCategoryParentId());
            entity.setRemark(dto.getRemark());

            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            projectCategoryMapperExt.updateByPrimaryKeySelective(entity);
        } else {

            //判断是否存存在
            if (checkExist(dto.getProjectCategoryCode())) {
                throw new BusinessException("项目分类Code已存在");
            }
            entity = new ProjectCategory();
            entity.setProjectCategoryCode(dto.getProjectCategoryCode());
            entity.setProjectCategoryName(dto.getProjectCategoryName());
            entity.setProjectCategoryNameZw(dto.getProjectCategoryNameZw());
            entity.setProjectCategoryParentId(dto.getProjectCategoryParentId());
            entity.setRemark(dto.getRemark());

            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            projectCategoryMapperExt.insertSelective(entity);
        }
        dto = ProjectCategoryDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个项目分类信息
    public Boolean delete(ProjectCategory entity) {
        entity.setIsDelete(true);
        projectCategoryMapperExt.updateByPrimaryKeySelective(entity);

        //recursive
        List<ProjectCategory> children = getAllChildren(entity.getProjectCategoryId());
        if (children != null && children.size() > 0) {
            for (ProjectCategory child : children) {
                delete(child);
            }
        }
        return true;
    }

    public Boolean delete(Integer projectCategoryId) {
        ProjectCategory entity = projectCategoryMapperExt.selectByPrimaryKey(projectCategoryId);
        return delete(entity);
    }

    public List<Integer> allIds(){
        ProjectCategoryExample example = new ProjectCategoryExample();
        ProjectCategoryExample.Criteria criteria= example.createCriteria();
        criteria.andIsDeleteEqualTo(false);

        List<Integer> ids = new ArrayList<>();
        List<ProjectCategory> list = projectCategoryMapperExt.selectByExample(example);
        for (ProjectCategory entity:list){
            ids.add(entity.getProjectCategoryId());
        }

        return ids;
    }

    //不分页全部查询项目分类信息列表
    public List<ProjectCategoryDTO> all(ProjectCategorySearchDTO projectCategorySearchDTO) {
        ProjectCategoryExample example = buildSearchExample(projectCategorySearchDTO);
        List<ProjectCategoryDTO> data = new ArrayList<ProjectCategoryDTO>();
        List<ProjectCategory> list = projectCategoryMapperExt.selectByExample(example);
        for (ProjectCategory entity : list) {
            ProjectCategoryDTO dto = ProjectCategoryDomainConvertUtils.convert(entity);
            List<ProjectCategory> childrenList = getAllChildren(dto.getProjectCategoryId());
            if (childrenList != null && childrenList.size() > 0) {
                List<ProjectCategoryDTO> childrenData = new ArrayList<ProjectCategoryDTO>();
                for (ProjectCategory category : childrenList) {
                    ProjectCategoryDTO childDto = ProjectCategoryDomainConvertUtils.convert(category);
                    ProjectCategorySearchDTO childSearchDTO = new ProjectCategorySearchDTO();
                    childSearchDTO.setProjectCategoryParentId(category.getProjectCategoryId());

                    childDto.setChildren(all(childSearchDTO));
                    childrenData.add(childDto);
                }
                dto.setChildren(childrenData);
            }
            data.add(dto);
        }
        return data;
    }

    /**
     * 获取某一category的子category
     *
     * @param projectCategoryId
     * @return
     */
    public List<ProjectCategory> getAllChildren(Integer projectCategoryId) {
        ProjectCategoryExample example = new ProjectCategoryExample();
        ProjectCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andProjectCategoryParentIdEqualTo(projectCategoryId);
        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        List<ProjectCategory> childrenList = projectCategoryMapperExt.selectByExample(example);
        return childrenList;
    }

    //build查询Example
    private ProjectCategoryExample buildSearchExample(ProjectCategorySearchDTO search) {
        ProjectCategoryExample example = new ProjectCategoryExample();
        ProjectCategoryExample.Criteria criteria = example.createCriteria();

        if (search.getProjectCategoryId() != null && search.getProjectCategoryId() > 0) {
            criteria.andProjectCategoryIdEqualTo(search.getProjectCategoryId());
        }
        if (search.getProjectCategoryParentId() != null && search.getProjectCategoryParentId() >= 0) {
            criteria.andProjectCategoryParentIdEqualTo(search.getProjectCategoryParentId());
        }

        if (StringUtils.isNotBlank(search.getProjectCategoryCode())) {
            criteria.andProjectCategoryCodeEqualTo(search.getProjectCategoryCode());
        }
        if (StringUtils.isNotBlank(search.getProjectCategoryName())) {
            criteria.andProjectCategoryNameEqualTo(search.getProjectCategoryName());
        }

        criteria.andIsDeleteEqualTo(Boolean.FALSE);
        if (StringUtils.isNotBlank(search.getOrderByClause())) {
            example.setOrderByClause(search.getOrderByClause());
        } else {
            example.setOrderByClause("project_category_id asc");
        }
        return example;
    }

}
